package com.infore.siteManagement.service.impl;

import java.util.Arrays;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.infore.siteManagement.constant.Constants;
import com.infore.siteManagement.constant.ResultConstants;
import com.infore.siteManagement.entity.EquipmentType;
import com.infore.siteManagement.enums.ResultStatus;
import com.infore.siteManagement.repository.IEquipmentTypeRespository;
import com.infore.siteManagement.select.QueryDispatcher;
import com.infore.siteManagement.select.dto.QueryParamDto;
import com.infore.siteManagement.select.util.QueryUtil;
import com.infore.siteManagement.service.IEquipmentTypeService;
import com.infore.siteManagement.util.MyBeanUtils;
import com.infore.siteManagement.util.Page;
import com.infore.siteManagement.util.PageUtil;
import com.infore.siteManagement.util.ResultEntity;
/**
 * 设备类型管理服务实现层
 * @author 刘乐法
 *
 *
 */
@Service
public class IEquipmentTypeServiceImpl implements IEquipmentTypeService{
	
	@Autowired
	private IEquipmentTypeRespository iEquipmentTypeRespository ;
	@Autowired
	private QueryDispatcher queryDispatcher;

	@Override
	public ResultEntity queryEquipmentType(QueryParamDto queryParamDto) {
		if (StringUtils.isEmpty(queryParamDto)) {//无任何条件查询
			List<EquipmentType> list = iEquipmentTypeRespository.findAll();
			return new ResultEntity(ResultStatus.SUCCESS,Constants.QUERY_SUCCESS).setDataList(list);
		//仅分页查询
		}else if (!StringUtils.isEmpty(queryParamDto.getPageObject()) && 
				StringUtils.isEmpty(queryParamDto.getQueryCondition()) &&
				StringUtils.isEmpty(queryParamDto.getAssembleCondition()) && 
				StringUtils.isEmpty(queryParamDto.getAssembleSql())) {
			org.springframework.data.domain.Page<EquipmentType> pageable = iEquipmentTypeRespository.findAll(queryParamDto.getPageObject().getPageRequest());
			Page<EquipmentType> page = PageUtil.pageConvert(pageable);
			return new ResultEntity(ResultStatus.SUCCESS,Constants.QUERY_SUCCESS).setPage(page);
		//只传属性不传字段的查询
		}else if (org.apache.commons.lang.StringUtils.isBlank(queryParamDto.getAssembleSql().getSelect())) {
			return this.queryAll(queryParamDto);
		}
		//指定字段查询
		return queryDispatcher.dispatcher(queryParamDto, EquipmentType.class);
	}
	
	@SuppressWarnings("serial")
	public ResultEntity queryAll(QueryParamDto queryParamDto) {
		//判断是否有条件再判断是否需要分页,不分页必须传null
		if (!StringUtils.isEmpty(queryParamDto.getPageObject())) {
			//需要分页
			org.springframework.data.domain.Page<EquipmentType> pageAble =  iEquipmentTypeRespository.findAll(new Specification<EquipmentType>() {

				@Override
				public Predicate toPredicate(Root<EquipmentType> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
					//构建条件
					List<Predicate> predicates = com.infore.siteManagement.select.util.QueryUtil.
							createQueryCondition(queryParamDto, criteriaBuilder, root);
					//组装条件
					Predicate predicate = com.infore.siteManagement.select.util.QueryUtil.
							assembleCondition(queryParamDto, criteriaBuilder, predicates);
					QueryUtil.assembleSql(queryParamDto, criteriaBuilder, query, root, predicate);
					return query.getRestriction();
				}
			}, queryParamDto.getPageObject().getPageRequest());
			Page<EquipmentType> page = PageUtil.pageConvert(pageAble);
			return new ResultEntity(ResultStatus.SUCCESS,Constants.QUERY_SUCCESS).setPage(page);
		}else {
			List<EquipmentType> list = iEquipmentTypeRespository.findAll(new Specification<EquipmentType>() {

				@Override
				public Predicate toPredicate(Root<EquipmentType> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
					//构建条件
					List<Predicate> predicates = com.infore.siteManagement.select.util.QueryUtil.
							createQueryCondition(queryParamDto, criteriaBuilder, root);
					//组装条件
					Predicate predicate = com.infore.siteManagement.select.util.QueryUtil.
							assembleCondition(queryParamDto, criteriaBuilder, predicates);
					QueryUtil.assembleSql(queryParamDto, criteriaBuilder, query, root, predicate);
					return query.getRestriction();
				}
			});
			return new ResultEntity(ResultStatus.SUCCESS,Constants.QUERY_SUCCESS).setDataList(list);
		}
	}

	@Override
	public ResultEntity deleteEquipmentType(String ids) {
		if (ids.contains(",")) {
			List<String> idsList = Arrays.asList(ids.split(","));
			iEquipmentTypeRespository.deleteByEquipmentTypeIdIn(idsList);
		} else {
			iEquipmentTypeRespository.deleteById(ids);
		}
		return new ResultEntity(ResultStatus.SUCCESS,Constants.DELETE_SUCCESS);
	}

	@Override
	public ResultEntity editEquipmentType(EquipmentType equipmenttype) {
		//flag用来判断返回的Constants.UPDATE_SUCCESS还是Constants.INSERT_SUCCESS
		Boolean falg = false;//编辑为true新建false
		//如果id为空进行新增，不为空则进行编辑
		if (!StringUtils.isEmpty(equipmenttype.getEquipmentTypeId())) {
			EquipmentType oldEquipmenttype = iEquipmentTypeRespository.findById(equipmenttype.getEquipmentTypeId()).orElse(null);
			//若id存在，判断数据库中对象是否存在
			if (!StringUtils.isEmpty(oldEquipmenttype)) {
				//防止编辑时传入null值覆盖原有数据库中的值
				MyBeanUtils.copyNonNullProperties(equipmenttype, oldEquipmenttype);
				equipmenttype = oldEquipmenttype;
			} else {
				return new ResultEntity(ResultStatus.ERROR,ResultConstants.NO_SUCH_OBJECT);
			}
			falg = true;
		}
		//验证设备类型
		ResultEntity resultEntity = validateEquipmentType(equipmenttype);
		if (resultEntity.getCode() == 1) {
			return resultEntity;
		}
		try {
			iEquipmentTypeRespository.saveAndFlush(equipmenttype);
		} catch (Exception e) {
			e.printStackTrace();
		}
		if (falg) {
			return new ResultEntity(ResultStatus.SUCCESS,Constants.UPDATE_SUCCESS);
		}else {
			return new ResultEntity(ResultStatus.SUCCESS,Constants.INSERT_SUCCESS);
		}
	}

	@Override
	public ResultEntity validateEquipmentType(EquipmentType equipmenttype) {
		String message = "设备类型验证成功";
		if (equipmenttype.getEquipmentTypeCode().length() > 10) {
			message = "设备类型编码过长";
		}else if (equipmenttype.getEquipmentTypeName().length() > 50) {
			message = "设备类型名称过长";
		}else if (equipmenttype.getRemark().length() > 50) {
			message = "设备类型备注过长";
		}else if (equipmenttype.getLastUpdatePeople().length() > 32) {
			message = "最新修改人名称过长";
		}else if (equipmenttype.getCreatePeople().length() > 32) {
			message = "创始人名称过长";
		}
		if (("设备类型验证成功").equals(message)) {
			return new ResultEntity(ResultStatus.SUCCESS,message);
		} else {
			return new ResultEntity(ResultStatus.ERROR,message);
		}
	}

}
